import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useAppStore = defineStore('app', () => {
  const loading = ref(false)
  const sidebarCollapsed = ref(false)
  const theme = ref<'light' | 'dark'>('light')
  const locale = ref('zh-CN')

  // 设置加载状态
  const setLoading = (status: boolean) => {
    loading.value = status
  }

  // 切换侧边栏
  const toggleSidebar = () => {
    sidebarCollapsed.value = !sidebarCollapsed.value
  }

  // 设置主题
  const setTheme = (newTheme: 'light' | 'dark') => {
    theme.value = newTheme
    localStorage.setItem('theme', newTheme)
  }

  // 设置语言
  const setLocale = (newLocale: string) => {
    locale.value = newLocale
    localStorage.setItem('locale', newLocale)
  }

  // 初始化应用设置
  const initApp = () => {
    const savedTheme = localStorage.getItem('theme') as 'light' | 'dark'
    const savedLocale = localStorage.getItem('locale')

    if (savedTheme) {
      theme.value = savedTheme
    }

    if (savedLocale) {
      locale.value = savedLocale
    }
  }

  return {
    loading,
    sidebarCollapsed,
    theme,
    locale,
    setLoading,
    toggleSidebar,
    setTheme,
    setLocale,
    initApp,
  }
})
